package controllers.cutter;

import hprose.HproseClinetService;

import java.io.File;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import utils.convert.Convert;
import utils.file.BatchImport;
import utils.file.DownExcel;
import utils.https.HttpRequest;
import utils.logs.LogsUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import component.UserBis;
import constants.APIConstants;
import constants.Constants;
import controllers.common.filter.BaseFilter;

public class CutterManageAction extends BaseFilter {

	/**
	 * 刀具管理
	* @Title: index 
	* @Description: TODO(这里用一句话描述这个方法的作用) 
	* @param     设定文件 
	* @return void    返回类型 
	* @author chasel 641243343@qq.com   
	* @throws
	 */
	public static void index(String rsMsg){
		if (StringUtils.isBlank(rsMsg)) {
			render();
		}else{
			render(rsMsg);
		}
	}
	/**
	 * 查看刀具列表
	* @Title: getCutterList 
	* @Description: TODO(这里用一句话描述这个方法的作用) 
	* @param     设定文件 
	* @return void    返回类型 
	* @author chasel 641243343@qq.com   
	* @throws
	 */
	public static void getCutterList(){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("customer_id", UserBis.currUser());
		map.put("cust_cutter_spesc_id", params.get("cust_cutter_spesc_id"));
		map.put("cutter_specs", params.get("cutter_specs"));
		map.put("supp_name", params.get("supp_name"));
		map.put("currPage", params.get("currPage"));
		map.put("pageSize", params.get("pageSize"));
		map.put("isPage", Constants.DATAISPAGE_YES);
		map.put("status", "1");
		Object[] arguments = new Object[]{map};
		String cutterspesc = HproseClinetService.synCall(APIConstants.CSMI_GETCUTTERSPECS, arguments,APIConstants.CUSTOMER_HOST, APIConstants.CUSTOMER_PORT);
		JSONObject json = JSONObject.parseObject(cutterspesc);
		renderJSON(json);
	
	}
	/**
	 * 添加刀具
	* @Title: addCutter 
	* @Description: TODO(这里用一句话描述这个方法的作用) 
	* @param     设定文件 
	* @return void    返回类型 
	* @author chasel 641243343@qq.com   
	* @throws
	 */
	public static void addCutter(){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("customer_id", UserBis.currUser());
		map.put("cust_cutter_spesc_id", params.get("cust_cutter_spesc_id"));
		map.put("cutter_specs", params.get("cutter_specs"));
		map.put("supp_name", params.get("supp_name"));
		map.put("safeNum", params.get("safeNum"));
		map.put("simple_specs_cust", params.get("simple_specs_cust"));
		Object[] arguments = new Object[]{map};
		LogsUtils.addLogs(Constants.LOG_TYPE_ADD,Convert.convertToInt(UserBis.currOperatorId(),-1), 1, "添加客户刀具执行","success", UserBis.currLoginAccount(), UserBis.currUserName(), UserBis.currUser());
		String cutterspesc = HproseClinetService.synCall(APIConstants.CSMI_ADDCUTTERSPECS, arguments,APIConstants.CUSTOMER_HOST, APIConstants.CUSTOMER_PORT);
		renderText(cutterspesc);
	}
	/**
	 * 删除刀具
	* @Title: delCutter 
	* @Description: TODO(这里用一句话描述这个方法的作用) 
	* @param     设定文件 
	* @return void    返回类型 
	* @author chasel 641243343@qq.com   
	* @throws
	 */
	public static void delCutter(){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("customer_id", UserBis.currUser());
		map.put("material_id", params.get("material_id"));
		Object[] arguments = new Object[]{map};
		LogsUtils.addLogs(Constants.LOG_TYPE_DEL,Convert.convertToInt(UserBis.currOperatorId(),-1), 1, "删除客户刀具执行","success", UserBis.currLoginAccount(), UserBis.currUserName(), UserBis.currUser());
		String cutterspesc = HproseClinetService.synCall(APIConstants.CSMI_DELETECUTTERSPECS, arguments,APIConstants.CUSTOMER_HOST, APIConstants.CUSTOMER_PORT);
		renderText(cutterspesc);
	}
	/**
	 * 修改刀具信息
	* @Title: editCutter 
	* @Description: TODO(这里用一句话描述这个方法的作用) 
	* @param     设定文件 
	* @return void    返回类型 
	* @author chasel 641243343@qq.com   
	* @throws
	 */
	public static void editCutter(){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("customer_id", UserBis.currUser());
		map.put("material_id", params.get("material_id"));
		map.put("simple_specs_cust", params.get("simple_specs_cust"));
		map.put("cust_cutter_spesc_id", params.get("cust_cutter_spesc_id"));
		map.put("supp_name", params.get("supp_name"));
		map.put("safeNum", params.get("safeNum"));
		Object[] arguments = new Object[]{map};
		LogsUtils.addLogs(Constants.LOG_TYPE_DEL,Convert.convertToInt(UserBis.currOperatorId(),-1), 1, "修改客户刀具执行","success", UserBis.currLoginAccount(), UserBis.currUserName(), UserBis.currUser());
		String cutterspesc = HproseClinetService.synCall(APIConstants.CSMI_EDITCUTTERSPECS, arguments,APIConstants.CUSTOMER_HOST, APIConstants.CUSTOMER_PORT);
		renderText(cutterspesc);
	}
	public static void upload(File myfile){
		List list = BatchImport.getAllByExcel(myfile); 
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("customer_id", UserBis.currUser());
		paramMap.put("list", list);
		if(list.size() > 1000){
			index("当前Excel行数："+list.size()+";请删除空行,并且行数不超过1000行");
		}
		Object[] arguments = new Object[]{paramMap};
		String res = HproseClinetService.synCall(APIConstants.CSMI_ADDCUTTERSPECSEXCEL, arguments,APIConstants.CUSTOMER_HOST, APIConstants.CUSTOMER_PORT);

		JSONObject json = JSONObject.parseObject(res);
		

		index(json.get("msg").toString());
		
	}
	
	public static void download(){
		HSSFWorkbook wb = DownExcel.downExcel(null, "规格:规格简称:供应商:安全数量",null, null);
        String fileName = "AddCutter.xls";
        try {
            response.setContentTypeIfNotSet("application/vnd.ms-excel");
            response.setHeader("content-disposition", "attachment;filename="
                    + URLEncoder.encode(fileName, "GB2312"));
            OutputStream out = response.out;
            wb.write(out);
        } catch (Exception e) {
            e.printStackTrace();
        } 
	}
	
	public static void addSupplierCutterInit(){
		 render();
	}
	 /**
	  * 获取客户绑定的供应商列表
	  */
	public static void getSupplierList(){
		JSONObject supplierList = new JSONObject();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("start", 0);
		map.put("length", 9999);
		String res;
		try {
			res = HttpRequest.getInstance().doGet(APIConstants.SUPPLIER_LIST+UserBis.currUser(), map); 
			supplierList = JSONObject.parseObject(res);
		} catch (Exception e) {
			e.printStackTrace();
		}
		JSONObject resultObj = supplierList.getJSONObject("data");
		renderJSON(resultObj);
	}
	
	public static void getSupplierCutterById(String supplier_id){
		JSONObject supplierList = new JSONObject();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("start", 0);
		map.put("length", 9999);
		String res;
		try {
			map.put("supplierId", supplier_id);
			res = HttpRequest.getInstance().doGet(APIConstants.MILL_LIST+UserBis.currUser(), map); 
			supplierList = JSONObject.parseObject(res);
		} catch (Exception e) {
			e.printStackTrace();
		}
		JSONObject resultObj = supplierList.getJSONObject("data");
		renderJSON(resultObj);
	}
	
	/**
	 * 查看 供应商 所有刀具
	 */
	public static void getSupplierCutterList(String supplier_id){
		JSONObject supplierList = new JSONObject();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("start", 0);
		map.put("length", 9999);
		String res;
		try {
			map.put("supplierId", supplier_id);
			map.put("status", 3);
			res = HttpRequest.getInstance().doGet(APIConstants.SUPPLIER_MILL_LIST, map); 
			supplierList = JSONObject.parseObject(res);
		} catch (Exception e) {
			e.printStackTrace();
		}
		JSONObject resultObj = supplierList.getJSONObject("data");
		renderJSON(resultObj);
	}
	public static void submitSupplierCutter(){
		Map<String, Object> supplierCutter  = new HashMap<String, Object>();
		JSONArray cutterArr = new JSONArray();
		JSONArray oldCutterArr = new JSONArray();
		String supplierId = params.get("supplier_id");
		String cutters = params.get("cutters");
		if(StringUtils.isNotBlank(cutters)){
			oldCutterArr = JSONArray.parseArray(cutters);
			for (int i = 0; i < oldCutterArr.size(); i++) {
				JSONObject obj = new JSONObject();
				JSONObject oldCutter = oldCutterArr.getJSONObject(i);
				obj.put("millId",oldCutter.getString("millId"));
				obj.put("safeQty", Convert.convertToInt(oldCutter.getString("safeQty"),0));
				obj.put("shortName", oldCutter.getString("customerShortName"));
				obj.put("longName", oldCutter.getString("customerLongName"));
				obj.put("materialCode", oldCutter.getString("customerMaterialCode"));
				cutterArr.add(obj);
			}
			supplierCutter.put("customerId", UserBis.currUser());
			supplierCutter.put("supplierId", supplierId);
			supplierCutter.put("mills", cutterArr);
			try {
				String res = HttpRequest.getInstance().doPost(APIConstants.MILL_CUSTOMER,supplierCutter);
				JSONObject result = JSONObject.parseObject(res);
				if(result.getString("status").equals("201")){
					Map<String, Object> map = new HashMap<String, Object>();
					Object[] arguments = new Object[]{map};
					map.put("customer_id", UserBis.currUser());
					map.put("supplier_id", supplierId);
					map.put("cutters", oldCutterArr.toString());
					String apiRes = HproseClinetService.synCall(APIConstants.CSMI_BINDSUPPLERCUTTER, arguments,APIConstants.CUSTOMER_HOST, APIConstants.CUSTOMER_PORT);
					LogsUtils.addLogs(Constants.LOG_TYPE_ADD,Convert.convertToInt(UserBis.currOperatorId(),-1), 1, "添加客户刀具执行","success", UserBis.currLoginAccount(), UserBis.currUserName(), UserBis.currUser());
				}
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
	}
	/***
	 * 获取供应商刀具详细信息
	 */
	public static void getSupperCutterDetails(){
		
	}
	
	/**
	 * 添加使用寿命区间
	 * 
	 * 2016年7月15日 上午9:38:32
	 * @auth liqi
	 */
	public static void addCutterSpecsInterval(){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("customer_id", UserBis.currUser());
		map.put("data", params.get("reData"));
		map.put("material_id", params.get("material_id"));
		Object[] arguments = new Object[]{map};
		String rs = HproseClinetService.synCall(APIConstants.CSMI_ADDCUTTERSPECSINTERVAL, arguments,APIConstants.CUSTOMER_HOST, APIConstants.CUSTOMER_PORT);
		renderText(rs);
	}
	
	/**
	 * 查询使用寿命区间
	 * 
	 * 2016年7月15日 下午5:10:19
	 * @auth liqi
	 */
	public static void getCutterSpecsInterval(){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("customer_id", UserBis.currUser());
		map.put("material_id", params.get("material_id"));
		Object[] arguments = new Object[]{map};
		String rs = HproseClinetService.synCall(APIConstants.CSMI_GETCUTTERSPECSINTERVAL, arguments,APIConstants.CUSTOMER_HOST, APIConstants.CUSTOMER_PORT);
		renderText(rs);
	}
}

